package com.mirei.listener;

import com.alibaba.fastjson.JSONObject;
import com.mirei.entity.CustomMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author topic模式绑定关系与监听
 */
@Slf4j
@Component
public class TopicListener {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "boot.topic.news"),
            exchange = @Exchange(name = "boot.topic", type = ExchangeTypes.TOPIC),
            key = {"news.#"}))
    public void listen1(CustomMessage msg){
        log.info("消费者收到[boot.topic.news]队列的消息,内容:{}", msg.getBody());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "boot.topic.weather"),
            exchange = @Exchange(name = "boot.topic", type = ExchangeTypes.TOPIC),
            key = {"*.weather.#"}))
    public void listen2(CustomMessage msg){
        log.info("消费者收到[boot.topic.weather]队列的消息,内容:{}", msg.getBody());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "boot.topic.json"),
            exchange = @Exchange(name = "boot.topic", type = ExchangeTypes.TOPIC),
            key = {"json"}))
    public void listen3(Message msg){
        log.info("消费者收到[boot.topic.json]队列的消息,内容:{}", JSONObject.toJSONString(msg));
    }
}
